﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;


namespace RemoteHardwareMonitor
{
    /// <summary>
    /// Provide access to OpenHardwareMonitor CPU and GPU data
    /// </summary>
    class HardwareMonitorProvider
    {

        public HardwareMonitorProvider()
        {
            
        }

            Computer computer = new Computer( );
            HardwareVisitor visitor = new HardwareVisitor();
            private IHardware[] _CPUs;
            private IHardware[] _GPUs;

            private int _cpus = 0;
            private int _gpus = 0;

            public IHardware[] CPUs
            {
                get { return _CPUs; }
            }

            public IHardware[] GPUs
            {
                get { return _GPUs; }
            }

            public int CPUCount
            {
                get { return _cpus; }
            }

            public int GPUCount
            {
                get { return _gpus; }
            }

            public delegate void SuccessfulHardwarePoll(object sender, EventArgs e);
            public event SuccessfulHardwarePoll OnSuccessfulHardwarePoll = delegate { };

            public delegate void SuccessfulinitialPoll(object sender, EventArgs e);
            public event SuccessfulinitialPoll OnSuccessfulInitialPoll = delegate { };
        /// <summary>
        /// Method suitable to be accessed via a TimerCallback
        /// </summary>
        /// <param name="state"></param>
            public void Poll(object state)
            {
                try
                {

                    computer.Open();
                    computer.Accept(visitor);

                    int cpus = 0;
                    int gpus = 0;
                    foreach (IHardware hardware in computer.Hardware)
                    {
                        switch (hardware.HardwareType)
                        {
                            case HardwareType.CPU:
                                _CPUs[cpus] = hardware;
                                cpus++;
                                break;

                            case HardwareType.GpuAti:
                                _GPUs[gpus] = hardware;
                                gpus++;
                                break;
                            case HardwareType.GpuNvidia:
                                _GPUs[gpus] = hardware;
                                gpus++;
                                break;
                        }
                    }
#if DEBUG
                    Console.WriteLine("hardware poll.");
#endif
                    OnSuccessfulHardwarePoll(this, null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    computer.Close();
                }
            }

            public void InitialPoll()
            {
                int cpus = 0;
                int gpus = 0;
                try
                {

                    computer.Open();
                    computer.Accept(visitor);

                    foreach (IHardware hardware in computer.Hardware)
                    {
                        switch (hardware.HardwareType)
                        {
                            case HardwareType.CPU:
                                cpus++;
                                break;

                            case HardwareType.GpuAti:
                                gpus++;
                                break;
                            case HardwareType.GpuNvidia:
                                gpus++;
                                break;
                        }
                        Console.WriteLine("Found:");
                        Console.WriteLine(hardware.ToString());
                    }
                    _CPUs = new IHardware[cpus];
                    _GPUs = new IHardware[gpus];
                    _cpus = cpus;
                    _gpus = gpus;

                    Poll(null);

                    OnSuccessfulInitialPoll(this, null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    computer.Close();
                }
            }





    }
}
